#include <iostream>
#include <vector>
#include <string>
using namespace std;

vector<int> u;

int find(int x)
{
    if(u[x] < 0) return x;
    return u[x] = find(u[x]);
}

void merge(int a, int b)
{
    if(a == b) return;
    int p1 = find(a), p2 = find(b);
    if(p1 == p2) return;
    u[p1] += u[p2];
    u[p2] = p1;
}

int main()
{
    int n, m, a, b; cin >> n >> m;
    u.resize(n, -1);
    
    while(m--)
    {
        string str; cin >> str;
        if(str == "C") {
            cin >> a >> b; a--, b--;
            merge(a, b);
        } else if (str == "Q1") {
            cin >> a >> b; a--, b--;
            int p1 = find(a), p2 = find(b);
            if(p1 == p2 && p1 >= 0) cout << "Yes\n";
            else cout << "No\n";
        } else {
            cin >> a; a--;
            cout << -u[find(a)] << endl;
        }
        
    }
    return 0;
}